package com.laivi.basic.module.syncdata;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;

import com.laivi.basic.common.constants.Constant;
import com.laivi.basic.model.syncdata.DestDatabase;
import com.laivi.basic.model.syncdata.SrcDatabase;
import com.laivi.basic.type.Status;
import com.laivi.basic.type.SyncType;
import com.laivi.basic.util.DataUtil;

public class SynData {
	private Timer timer;
	private Map<String,Task> taskMap=new HashMap<String,Task>();
	private List<Task> tasks;
	
	public SynData() {
		super();
		timer=new Timer();
	}
	
	public SynData(List<Task> tasks) {
		this();
		this.tasks = tasks;
	}
	
	public void run(){
		for(Task task:this.tasks){
			if(!DataUtil.isEmpty(task.getName())){
				start(task);
			}
		}
	}
	
	public void run(Task task){
		if(!DataUtil.isEmpty(task.getName()) && taskMap.get(task.getName())==null){
			start(task);
		}
	}
	
	public void run(Task...tasks){
		for(Task task:tasks){
			if(!DataUtil.isEmpty(task.getName())){
				start(task);
			}
		}
	}
	
	public void start(Task task){
		if(task.getPeriod()!=0){
			timer.schedule(task, Constant.Time.SECOND, task.getPeriod());
			task.setStatus(Status.RUNNING);
			taskMap.put(task.getName(), task);
		}else{
			timer.schedule(task, 5*Constant.Time.SECOND);
		}
		
	}
	
	public void stop(String key){
		taskMap.get(key).stop();
	}
	
	public void stop(){
		for(Map.Entry<String, Task>entry:taskMap.entrySet()){
			entry.getValue().stop();
		}
		timer.cancel();
		timer=new Timer();
		taskMap.clear();
		tasks.clear();
	}
	
	
	public static void main(String[] args){
		//**********************************Mysql Demo*************************************************
		/*SrcDatabase src=new SrcDatabase("jdbc:mysql://localhost:3306/sic","root","123456");
		src.setSql("select * from sic_article where createDate > ?");
		src.setKeyField("createDate");
		
		DestDatabase dest= new DestDatabase("jdbc:mysql://localhost:3306/sicnutz","root","123456");
		dest.setTable("sic_article");
		
		DBToDBTask task=new DBToDBTask("articleSynTask",Constant.Time.DAY,src,dest);
		
		SynData sync=new SynData();
		
		sync.run(task);*/
		//**********************************sqlserver Demo*************************************************
		SrcDatabase src=new SrcDatabase("jdbc:jtds:sqlserver://10.200.60.110:1433/ARSystem","ARADMIN","AR#Admin#");
		src.setSql("select HostName,AppSysName,CabinetLocation,ManagementIP,BareCapacity,TotalSpace,UsedSpace,Region,CIStatus,'2014-08-14' as f_date from BMC_CORE_SPDB_DTG order by ManagementIP");
		
		SrcDatabase src1=new SrcDatabase("jdbc:jtds:sqlserver://10.200.60.110:1433/ARSystem","ARADMIN","AR#Admin#");
		src1.setSql("select CIStatus,AppSysName,HostName,SN,Cabinets,CabinetLocation,RuningIP,'2014-07-14' as f_date from BMC_CORE_SPDB_SVR order by SN");
		
		SrcDatabase src2=new SrcDatabase("jdbc:jtds:sqlserver://10.200.60.115:1433/ARSystem","ARADMIN","AR#Admin#");
		src2.setSql("select Contact_Company,last_name,Description,Assigned_Support_Company,Assigned_Support_Organization,Assigned_Group,Assignee,Status,Reported_Date,'分行' as type from HPD_Help_Desk where Contact_Company like '%分行' and Assigned_Support_Company like '%分行' order by Reported_Date");
		
		SrcDatabase src3=new SrcDatabase("jdbc:jtds:sqlserver://10.200.60.115:1433/ARSystem","ARADMIN","AR#Admin#");
		src3.setSql("select Contact_Company,last_name,Description,Assigned_Support_Company,Assigned_Support_Organization,Assigned_Group,Assignee,Status,Reported_Date,'传信系统' as type from HPD_Help_Desk where cChr_ServerType like '%传信系统%' order by Reported_Date");
		
		SrcDatabase src4=new SrcDatabase("jdbc:jtds:sqlserver://10.200.60.110:1433/ARSystem","ARADMIN","AR#Admin#");
		src4.setSql("select DISTINCT EquipName,AppSysName,CONVERT(varchar(100), GETDATE(), 23) as f_time from SPDB_CMDB_Sublist_Net_LBAP where EquipName is not null order by EquipName");
		
		SrcDatabase src5=new SrcDatabase("jdbc:jtds:sqlserver://10.100.36.71:1433/DB_cs","spdb_dp","spdbdp");
		src5.setSql("select rtrim([机器名]) as f_machine,rtrim([使用情况]) as f_used_status,rtrim([IP地址]) f_ip,rtrim([端口号])  f_port,[启用标志] f_flag,[机房用机] f_used_machine,rtrim([权限]) f_auth from [机器表] order by f_machine");
		
		SrcDatabase src6=new SrcDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/pmis","spdb","spdb@1234");
		src6.setSql("select c.* from (select i.ITEM_NAME,i.ITEM_DATE,bdi.BUDGET_ITEM_NAME,bi.OLD_BUDGET_MONEY+bi.Append_OR_MODIFY Budget_Money,bi.USED_BUDGET_MONEY as Real_Money,bdi.BUDGET_LB from T_BUDGET_ITEM bi LEFT JOIN T_ITEM i on i.ITEM_ID=bi.ITEM_ID LEFT JOIN T_BUDGET_DETAIL_ITEM bdi on bi.BUDGET_ITEM_ID=bdi.BUDGET_ITEM_ID LEFT JOIN T_DEPARTMENT d on i.APPLICATION_DEPT=CONVERT(varchar(60),d.UNIT_ID) where i.ITEM_NAME is not null and bdi.BUDGET_ITEM_NAME is not null and (bdi.BUDGET_LB =1) and (d.UNIT_NAME like '%科技管理部%' or d.UNIT_NAME like '%总行业务支持%') union select i.ITEM_NAME,i.ITEM_DATE,bdi.BUDGET_ITEM_NAME,bi.OLD_BUDGET_MONEY+bi.Append_OR_MODIFY Budget_Money,bi.USED_BUDGET_MONEY as Real_Money,bdi.BUDGET_LB from T_BUDGET_ITEM bi LEFT JOIN T_ITEM i on i.ITEM_ID=bi.ITEM_ID LEFT JOIN T_BUDGET_DETAIL_ITEM bdi on bi.BUDGET_ITEM_ID=bdi.BUDGET_ITEM_ID LEFT JOIN T_DEPARTMENT d on i.APPLICATION_DEPT=CONVERT(varchar(60),d.UNIT_ID) LEFT JOIN t_cgxmxx cg on cg.CGXMXX_LXYSID=i.ITEM_ID where  cg.cgxmxx_xmmc like '年度维护%' and bdi.BUDGET_LB=-1 and (d.UNIT_NAME like '%科技管理部%' or d.UNIT_NAME like '%总行业务支持%'))c order by item_date");
		
		SrcDatabase src7=new SrcDatabase("jdbc:oracle:thin:@10.112.11.200:1521:BSAEDB","aud_chk_data","spdb1234");
		src7.setSql("select a.* ,b.mid from(select r.upperdeptname,r.deptname,r.capp_name,r.dvcgrpsoftclassname,r.dvcgrpsoftname,r.hostname,r.sadvcid,count(*),sum(case when  upper(r.attribute_short_value) like '%\\_\\_NON-COMPLIANT%' ESCAPE '\' then 1 else 0 end) noncomp,sum(case  when  upper(r.attribute_short_value) like '%\\_\\_COMPLIANT%' ESCAPE '\' then 1 else 0 end) comp,sav.dvc_type from ( select ugv.upperdeptname,ugv.deptname,ugv.capp_name,ugv.dvcgrpsoftclassname,dgv.dvcgrpsoftname,dgv.hostname,dgv.sadvcid,sca.attribute_short_value ,sca.attribute_name,substr(sca.attribute_name,1,instr(sca.attribute_name,'_',1,2)-1) as chkt from platform.ccsd_t_dvc_dcvgrp_view dgv,cmdb_data.sas_servers_base sb,(select t.upperdeptname,t.deptname,t.appid,t.capp_name,t.dvcgrpsoftclassname,t.dvcgrpsoftname,t.ccsddvcgrpid from platform.ccsd_t_user_grp_view t group by t.upperdeptname,t.deptname,t.appid,t.capp_name,t.dvcgrpsoftclassname,t.dvcgrpsoftname,t.ccsddvcgrpid) ugv left join(select * from cmdb_data.sas_server_cust_attributes sca where to_date(substr(sca.attribute_short_value, 1, 8), 'yyyy-MM-dd') >= to_date('2014-04-01','yyyy-MM-dd') and to_date(substr(sca.attribute_short_value, 1, 8), 'yyyy-MM-dd') < to_date('2014-04-30','yyyy-MM-dd') and sca.attribute_name like '%' || 'AUD' || '%RES' ) sca on ugv.dvcgrpsoftname like '%' || substr(sca.attribute_name,instr(sca.attribute_name,'_',1,1)+1,instr(sca.attribute_name,'_',1,1)-4)||'%' where ugv.ccsddvcgrpid=dgv.ccsddvcgrpid and dgv.sadvcid=sb.server_id and sb.item_id=sca.item_id  ) r, platform.portal_sa_devices_view sav  where r.sadvcid=sav.mid  and substr(r.attribute_short_value,0,14) in (select max(substr(attribute_short_value,0, 14)) from( select ugv.upperdeptname,ugv.deptname,ugv.capp_name,ugv.dvcgrpsoftclassname,dgv.dvcgrpsoftname,dgv.hostname,dgv.sadvcid,sca.attribute_short_value ,sca.attribute_name,substr(sca.attribute_name,1,instr(sca.attribute_name,'_',1,2)-1) as chkt from platform.ccsd_t_dvc_dcvgrp_view dgv,cmdb_data.sas_servers_base sb,(select t.upperdeptname,t.deptname,t.appid,t.capp_name,t.dvcgrpsoftclassname,t.dvcgrpsoftname,t.ccsddvcgrpid from platform.ccsd_t_user_grp_view t group by t.upperdeptname,t.deptname,t.appid,t.capp_name,t.dvcgrpsoftclassname,t.dvcgrpsoftname,t.ccsddvcgrpid) ugv left join (select * from cmdb_data.sas_server_cust_attributes sca where to_date(substr(sca.attribute_short_value, 1, 8), 'yyyy-MM-dd') >= to_date('2014-04-01','yyyy-MM-dd') and to_date(substr(sca.attribute_short_value, 1, 8), 'yyyy-MM-dd') < to_date('2014-04-30','yyyy-MM-dd') and sca.attribute_name like '%' || 'AUD' || '%RES') sca on ugv.dvcgrpsoftname like '%' || substr(sca.attribute_name,instr(sca.attribute_name,'_',1,1)+1,instr(sca.attribute_name,'_',1,1)-4)||'%' where ugv.ccsddvcgrpid=dgv.ccsddvcgrpid and dgv.sadvcid=sb.server_id and sb.item_id=sca.item_id  ) group by sadvcid) group by r.upperdeptname,r.deptname,sav.dvc_type,r.capp_name,r.dvcgrpsoftclassname,r.dvcgrpsoftname,r.hostname,r.sadvcid order by r.capp_name,r.dvcgrpsoftclassname ) a ,platform.portal_sa_devices_view b where a.hostname=b.system_name order by hostname ");
		
		SrcDatabase src8=new SrcDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/pmis","spdb","spdb@1234");
		src8.setSql("select c.CGXMXX_XMMC,c.CGXMXX_LXSJ,dd.DICTIONARY_DETAIL_NAME,c.CGXMXX_XMZL,c.CGXMXX_NCGJE from t_cgxmxx c left join T_DICTIONARY_DETAIL dd on c.CGXMXX_CGFS=dd.DICTIONARY_DETAIL_CODE left join T_DEPARTMENT t2 on c.CGXMXX_CGXQBM=CONVERT(varchar(60),t2.UNIT_ID) where dd.DICTIONARY_ID=25 and t2.UNIT_NAME like '%科技管理部%' order by cgxmxx_xmmc");
		
		SrcDatabase src9=new SrcDatabase("jdbc:jtds:sqlserver://10.200.60.110:1433/ARSystem","ARADMIN","AR#Admin#");
		src9.setSql("SELECT DISTINCT AppSysName,SVRName,AppSysMTCTI,AppSysCIOwnerA,AppSysCIOwnerB,AppSysCIOwnerC,AppSysCIOwnerD,SVRCIStatus,SVRType,SVRManufacturerName,CONVERT(varchar(100), GETDATE(), 23) as f_sampletime from APPsys_JOIN_SVR_JOIN_MAC_IP_DTG_SWITCH_PT_ALL order by AppSysName");
		
		
		/*
		SrcDatabase src6=new SrcDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/pmis","spdb","spdb@1234");
		src6.setSql("select i.ITEM_NAME,i.ITEM_DATE,bdi.BUDGET_ITEM_NAME,bi.OLD_BUDGET_MONEY+bi.Append_OR_MODIFY Budget_Money,bi.USED_BUDGET_MONEY as Real_Money,bdi.BUDGET_LB from T_BUDGET_ITEM bi LEFT JOIN T_ITEM i on i.ITEM_ID=bi.ITEM_ID LEFT JOIN T_BUDGET_DETAIL_ITEM bdi on bi.BUDGET_ITEM_ID=bdi.BUDGET_ITEM_ID LEFT JOIN T_DEPARTMENT d on i.APPLICATION_DEPT=CONVERT(varchar(60),d.UNIT_ID) where i.ITEM_NAME is not null and bdi.BUDGET_ITEM_NAME is not null and (bdi.BUDGET_LB =1 or bdi.BUDGET_LB=-1) and (d.UNIT_NAME like '%科技管理部%' or d.UNIT_NAME like '%总行业务支持%') order by ITEM_DATE");
		
		SrcDatabase src7=new SrcDatabase("jdbc:oracle:thin:@10.112.11.200:1521:BSAEDB","aud_chk_data","spdb1234");
		src7.setSql("select a.* ,b.mid from(select r.upperdeptname,r.deptname,r.capp_name,r.dvcgrpsoftclassname,r.dvcgrpsoftname,r.hostname,r.sadvcid,count(*),sum(case when  upper(r.attribute_short_value) like '%\\_\\_NON-COMPLIANT%' ESCAPE '\' then 1 else 0 end) noncomp,sum(case  when  upper(r.attribute_short_value) like '%\\_\\_COMPLIANT%' ESCAPE '\' then 1 else 0 end) comp,sav.dvc_type from ( select ugv.upperdeptname,ugv.deptname,ugv.capp_name,ugv.dvcgrpsoftclassname,dgv.dvcgrpsoftname,dgv.hostname,dgv.sadvcid,sca.attribute_short_value ,sca.attribute_name,substr(sca.attribute_name,1,instr(sca.attribute_name,'_',1,2)-1) as chkt from platform.ccsd_t_dvc_dcvgrp_view dgv,cmdb_data.sas_servers_base sb,(select t.upperdeptname,t.deptname,t.appid,t.capp_name,t.dvcgrpsoftclassname,t.dvcgrpsoftname,t.ccsddvcgrpid from platform.ccsd_t_user_grp_view t group by t.upperdeptname,t.deptname,t.appid,t.capp_name,t.dvcgrpsoftclassname,t.dvcgrpsoftname,t.ccsddvcgrpid) ugv left join(select * from cmdb_data.sas_server_cust_attributes sca where to_date(substr(sca.attribute_short_value, 1, 8), 'yyyy-MM-dd') >= to_date('2014-04-01','yyyy-MM-dd') and to_date(substr(sca.attribute_short_value, 1, 8), 'yyyy-MM-dd') < to_date('2014-04-30','yyyy-MM-dd') and sca.attribute_name like '%' || 'AUD' || '%RES' ) sca on ugv.dvcgrpsoftname like '%' || substr(sca.attribute_name,instr(sca.attribute_name,'_',1,1)+1,instr(sca.attribute_name,'_',1,1)-4)||'%' where ugv.ccsddvcgrpid=dgv.ccsddvcgrpid and dgv.sadvcid=sb.server_id and sb.item_id=sca.item_id  ) r, platform.portal_sa_devices_view sav  where r.sadvcid=sav.mid  and substr(r.attribute_short_value,0,14) in (select max(substr(attribute_short_value,0, 14)) from( select ugv.upperdeptname,ugv.deptname,ugv.capp_name,ugv.dvcgrpsoftclassname,dgv.dvcgrpsoftname,dgv.hostname,dgv.sadvcid,sca.attribute_short_value ,sca.attribute_name,substr(sca.attribute_name,1,instr(sca.attribute_name,'_',1,2)-1) as chkt from platform.ccsd_t_dvc_dcvgrp_view dgv,cmdb_data.sas_servers_base sb,(select t.upperdeptname,t.deptname,t.appid,t.capp_name,t.dvcgrpsoftclassname,t.dvcgrpsoftname,t.ccsddvcgrpid from platform.ccsd_t_user_grp_view t group by t.upperdeptname,t.deptname,t.appid,t.capp_name,t.dvcgrpsoftclassname,t.dvcgrpsoftname,t.ccsddvcgrpid) ugv left join (select * from cmdb_data.sas_server_cust_attributes sca where to_date(substr(sca.attribute_short_value, 1, 8), 'yyyy-MM-dd') >= to_date('2014-04-01','yyyy-MM-dd') and to_date(substr(sca.attribute_short_value, 1, 8), 'yyyy-MM-dd') < to_date('2014-04-30','yyyy-MM-dd') and sca.attribute_name like '%' || 'AUD' || '%RES') sca on ugv.dvcgrpsoftname like '%' || substr(sca.attribute_name,instr(sca.attribute_name,'_',1,1)+1,instr(sca.attribute_name,'_',1,1)-4)||'%' where ugv.ccsddvcgrpid=dgv.ccsddvcgrpid and dgv.sadvcid=sb.server_id and sb.item_id=sca.item_id  ) group by sadvcid) group by r.upperdeptname,r.deptname,sav.dvc_type,r.capp_name,r.dvcgrpsoftclassname,r.dvcgrpsoftname,r.hostname,r.sadvcid order by r.capp_name,r.dvcgrpsoftclassname ) a ,platform.portal_sa_devices_view b where a.hostname=b.system_name order by hostname ");
		
		SrcDatabase src8=new SrcDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/pmis","spdb","spdb@1234");
		src8.setSql("select c.CGXMXX_XMMC,c.CGXMXX_LXSJ,dd.DICTIONARY_DETAIL_NAME,c.CGXMXX_XMZL from t_cgxmxx c left join T_DICTIONARY_DETAIL dd on c.CGXMXX_CGFS=dd.DICTIONARY_DETAIL_CODE left join T_DEPARTMENT t2 on c.CGXMXX_CGXQBM=CONVERT(varchar(60),t2.UNIT_ID) where dd.DICTIONARY_ID=25 and t2.UNIT_NAME like '%科技管理部%' order by cgxmxx_xmmc");
		*/
		
		DestDatabase dest= new DestDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/spdbcore","spdb","spdb@1234");
		//dest.setType(SyncType.ALL);
		dest.setTable("t_config_disk");
		
		DestDatabase dest1= new DestDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/spdbcore","spdb","spdb@1234");
		dest1.setTable("t_config_hostApp");
		
		DestDatabase dest2= new DestDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/spdbcore","spdb","spdb@1234");
		dest2.setTable("t_fh_event");
		
		DestDatabase dest3= new DestDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/spdbcore","spdb","spdb@1234");
		dest3.setTable("t_fh_event");
		
		DestDatabase dest4= new DestDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/spdbcore","spdb","spdb@1234");
		dest4.setType(SyncType.ALL);
		dest4.setTable("t_lbap");
		
		DestDatabase dest5= new DestDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/spdbcore","spdb","spdb@1234");
		dest5.setType(SyncType.ALL);
		dest5.setTable("t_ecc_machine");
		
		DestDatabase dest6= new DestDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/spdbcore","spdb","spdb@1234");
		dest6.setType(SyncType.ALL);
		dest6.setTable("t_business_procurement");
		
		DestDatabase dest7= new DestDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/spdbcore","spdb","spdb@1234");
		dest7.setType(SyncType.ALL);
		dest7.setTable("t_chk");
		
		DestDatabase dest8= new DestDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/spdbcore","spdb","spdb@1234");
		dest8.setType(SyncType.ALL);
		dest8.setTable("t_cgxmxx");
		
		DestDatabase dest9= new DestDatabase("jdbc:jtds:sqlserver://10.112.11.125:1433/spdbcore","spdb","spdb@1234");
		dest9.setType(SyncType.ALL);
		dest9.setTable("t_appsys");
		
		DBToDBTask task=new DBToDBTask("configDisk",Constant.Time.DAY,src,dest);
		DBToDBTask task1=new DBToDBTask("configHostApp",Constant.Time.DAY,src1,dest1);
		DBToDBTask task2=new DBToDBTask("fhEvent",Constant.Time.DAY,src2,dest2);
		DBToDBTask task3=new DBToDBTask("cxEvent",Constant.Time.DAY,src3,dest3);
		DBToDBTask task4=new DBToDBTask("lbap",Constant.Time.DAY,src4,dest4);
		DBToDBTask task5=new DBToDBTask("eccMachine",Constant.Time.DAY,src5,dest5);
		
		DBToDBTask task6=new DBToDBTask("bussiness procurement",Constant.Time.DAY,src6,dest6);
		DBToDBTask task7=new DBToDBTask("chk",Constant.Time.DAY,src7,dest7);
		DBToDBTask task8=new DBToDBTask("cgxmxx",Constant.Time.DAY,src8,dest8);
		DBToDBTask task9=new DBToDBTask("appsys",Constant.Time.DAY,src9,dest9);
		
		SynData sync=new SynData();
		
		sync.run(task7);
	}
}
